{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lesson 02"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 题目1：有四个数字：1、2、3、4，能组成多少个互不相同且无重复数字的三位数？各是多少？     \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "程序分析：可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。\n",
    "程序源代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n",
      "1 2 4\n",
      "1 3 2\n",
      "1 3 4\n",
      "1 4 2\n",
      "1 4 3\n",
      "2 1 3\n",
      "2 1 4\n",
      "2 3 1\n",
      "2 3 4\n",
      "2 4 1\n",
      "2 4 3\n",
      "3 1 2\n",
      "3 1 4\n",
      "3 2 1\n",
      "3 2 4\n",
      "3 4 1\n",
      "3 4 2\n",
      "4 1 2\n",
      "4 1 3\n",
      "4 2 1\n",
      "4 2 3\n",
      "4 3 1\n",
      "4 3 2\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,5):\n",
    "    for j in range(1,5):\n",
    "        for k in range(1,5):\n",
    "            if( i != k ) and (i != j) and (j != k):\n",
    "                print(i,j,k)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 题目2：给你两个正整数a和b， 输出它们的最大公约数。\n",
    "\n",
    "例如：a = 24, b = 15\n",
    "\n",
    "则输出：3\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "a=24\n",
    "b=15\n",
    "for i in range(1,min(a,b)+1)[::-1]:\n",
    "    if (a%i==0 and b%i==0):\n",
    "        print(i)\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "while a*b!=0: \n",
    "    (a,b)=(b%a,a%b)\n",
    "print(a+b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 题目3：找到数字个数是1的编号，小编号在前，大编号在后，以一个空格隔开     \n",
    "\n",
    "例如：L=[1,1,4,4,7,5,5,3,6,6]\n",
    "则输出：3 7\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 7\n"
     ]
    }
   ],
   "source": [
    "L=[1,1,4,4,7,5,5,3,6,6]\n",
    "l=[]\n",
    "for item in L:\n",
    "    if L.count(item) == 1:\n",
    "        l.append(item)\n",
    "l.sort()\n",
    "print(l[0],l[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "##  题目4：输出 9*9 乘法口诀表\n",
    "\n",
    "\n",
    "\n",
    "程序分析：分行与列考虑，共9行9列，i控制行，j控制列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1 \t\n",
      "2*1=2 \t2*2=4 \t\n",
      "3*1=3 \t3*2=6 \t3*3=9 \t\n",
      "4*1=4 \t4*2=8 \t4*3=12 \t4*4=16 \t\n",
      "5*1=5 \t5*2=10 \t5*3=15 \t5*4=20 \t5*5=25 \t\n",
      "6*1=6 \t6*2=12 \t6*3=18 \t6*4=24 \t6*5=30 \t6*6=36 \t\n",
      "7*1=7 \t7*2=14 \t7*3=21 \t7*4=28 \t7*5=35 \t7*6=42 \t7*7=49 \t\n",
      "8*1=8 \t8*2=16 \t8*3=24 \t8*4=32 \t8*5=40 \t8*6=48 \t8*7=56 \t8*8=64 \t\n",
      "9*1=9 \t9*2=18 \t9*3=27 \t9*4=36 \t9*5=45 \t9*6=54 \t9*7=63 \t9*8=72 \t9*9=81 \t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):\n",
    "    for j in range(1, i+1):\n",
    "        print (\"%d*%d=%d\" % (i, j, i*j),\"\\t\",end=\"\")\n",
    "        if i==j:\n",
    "            print(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 x 1 = 1 \t\n",
      "1 x 2 = 2 \t2 x 2 = 4 \t\n",
      "1 x 3 = 3 \t2 x 3 = 6 \t3 x 3 = 9 \t\n",
      "1 x 4 = 4 \t2 x 4 = 8 \t3 x 4 = 12 \t4 x 4 = 16 \t\n",
      "1 x 5 = 5 \t2 x 5 = 10 \t3 x 5 = 15 \t4 x 5 = 20 \t5 x 5 = 25 \t\n",
      "1 x 6 = 6 \t2 x 6 = 12 \t3 x 6 = 18 \t4 x 6 = 24 \t5 x 6 = 30 \t6 x 6 = 36 \t\n",
      "1 x 7 = 7 \t2 x 7 = 14 \t3 x 7 = 21 \t4 x 7 = 28 \t5 x 7 = 35 \t6 x 7 = 42 \t7 x 7 = 49 \t\n",
      "1 x 8 = 8 \t2 x 8 = 16 \t3 x 8 = 24 \t4 x 8 = 32 \t5 x 8 = 40 \t6 x 8 = 48 \t7 x 8 = 56 \t8 x 8 = 64 \t\n",
      "1 x 9 = 9 \t2 x 9 = 18 \t3 x 9 = 27 \t4 x 9 = 36 \t5 x 9 = 45 \t6 x 9 = 54 \t7 x 9 = 63 \t8 x 9 = 72 \t9 x 9 = 81 \t\n"
     ]
    }
   ],
   "source": [
    "i=0\n",
    "j=0\n",
    "while i<9:\n",
    "    i+=1\n",
    "    while j<9:\n",
    "        j+=1\n",
    "        print(j,\"x\",i,\"=\",i*j,\"\\t\",end=\"\")\n",
    "        if i==j:\n",
    "            j=0\n",
    "            print(\"\")\n",
    "            break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
